package com.lizhaoshan.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.lizhaoshan.common.Result;
import com.lizhaoshan.dto.DishDto;
import com.lizhaoshan.pojo.Dish;
import com.lizhaoshan.pojo.PageBean;
import com.lizhaoshan.service.DishService;
import com.lizhaoshan.vo.DishVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * @program: reggie
 * @description:
 * @author: lamour
 * @create: 2022-05-14 19:53
 **/
@Slf4j
@RestController
@RequestMapping("dish")
public class DishController {
    @Autowired
    private DishService dishService;

    @PostMapping
    public Result addDish(@RequestBody DishDto dishDto){
        log.info("**********菜品添加，begin");

        if (Objects.isNull(dishDto)){
            return Result.error("暂无数据");
        }

        boolean result = dishService.addDish(dishDto);

        log.info("**********菜品添加,end");
        return result?Result.success("菜品添加成功"):Result.error("菜品添加失败");
    }

    @PutMapping
    public Result putDish(@RequestBody DishDto dishDto){
        log.info("**********菜品修改，begin");

        if (Objects.isNull(dishDto)){
            return Result.error("暂无数据");
        }

        boolean result = dishService.putDish(dishDto);

        log.info("**********菜品修改,end");
        return result?Result.success("菜品修改成功"):Result.error("菜品修改失败");
    }

    @DeleteMapping()
    public Result deleteDish(@RequestParam String ids){
        log.info("**********菜品删除，begin");

        String[] split = ids.split(",");
        List<String> idsList = Arrays.asList(split);

        boolean result = dishService.deleteByIds(idsList);
        log.info("**********菜品删除，end");
        return result?Result.success("删除成功"):Result.error("删除失败");
    }

    @PostMapping("/status/{status}")
    public Result updateStatus(@PathVariable Integer status,@RequestParam String ids){
        log.info("**********菜品起售修改，begin");

        String[] split = ids.split(",");
        List<String> idsList = Arrays.asList(split);

        boolean result = dishService.updateStatus(status,idsList);
        log.info("**********菜品起售修改，end");
        return result?Result.success("菜品起售修改成功"):Result.error("菜品起售修改失败");
    }

    @GetMapping("/list")
    public Result getListDish(@RequestParam String categoryId){
        log.info("**********菜品下拉查询，begin");
        if (StrUtil.isEmpty(categoryId)) {
            return Result.error("无菜品数据");
        }
        List<DishVo> dishVoList = dishService.getByCategoryId(categoryId);

        log.info("**********菜品下拉查询，end");
        return CollUtil.isEmpty(dishVoList)?Result.success("菜品下拉查询成功"):Result.error("菜品下拉查询失败");
    }

    @GetMapping("/page")
    public Result getPagesDish(@RequestParam(value = "page", defaultValue = "1") Integer page,
                               @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize,
                               @RequestParam(value = "type" , required = false ) String type,
                               @RequestParam(value = "name" , required = false ) String name){
        log.info("**********菜品分页查询，begin");
        PageBean<DishVo> dishVoPageBean = dishService.selectPage(page, pageSize, name,type);
        log.info("**********菜品分页查询，end");

        return Objects.nonNull(dishVoPageBean)?Result.success(dishVoPageBean):Result.error("菜品分页查询无数据");
    }

    @GetMapping("/{id}")
    public Result getByDishId(@PathVariable String id){
        log.info("**********通过菜品id查询，begin***********");
        DishVo dishVo = dishService.selectByDishId(id);
        log.info("**********通过菜品id查询，end***********");

        return Objects.nonNull(dishVo)?Result.success("通过菜品id查询成功"):Result.error("通过菜品id查询失败");
    }
}
